/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unitn.science.matr155863_148346_145963.bccastestore.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.PropertyConfigurator;


/**
 *
 * @author berto
 */
public class Log4JInitSRV extends HttpServlet {

    public void init(ServletConfig config) throws ServletException {
	System.out.println("Log4JInitSRV is initializing log4j");
	//Parametro file proprieties
	String log4jLocation = config.getInitParameter("log4j-properties-location");
	//Ottengo il contesto della servlet
	ServletContext sc = config.getServletContext();
	
	//Controllo parametro proprietis diverso da null
	if (log4jLocation == null) {
		//Se null DEFAULT
		System.err.println("*** No log4j-properties-location init param,"
                        + " so initializing log4j with BasicConfigurator");
		BasicConfigurator.configure();
	} else {
		//Carico le mie Impostazioni
		String webAppPath = sc.getRealPath("/");
		String log4jProp = webAppPath + log4jLocation;
		File fileProprietiesLog4J = new File(log4jProp);
		if (fileProprietiesLog4J.exists()) {
			System.out.println("Initializing log4j with: " + log4jProp);
			PropertyConfigurator.configure(log4jProp);
		} else {
			System.err.println("*** " + log4jProp + " file not found, "
                                + "so initializing log4j with BasicConfigurator");
			BasicConfigurator.configure();
		}
	}
	super.init(config);
    }
    
    private void howUse(){
        /* Descrizione presente dentro "Introduzione programmazione per il web"
         * Forse devo riumuovere il logger di java.utils dalla classe
        import org.apache.log4j.Logger;
        static Logger log = Logger.getLogger(NOME_DELLA_SERVLET_IN_CUI_LO_USO.class);
        
        log.debug("debug message");
        log.info("info message");
        log.warn("warn message");
        log.error("error message");
        log.fatal("fatal message");
        */
    }
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Init Log4";
    }// </editor-fold>
}
